#include <bits/stdc++.h>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
};

class Solution
{
public:
    int widthOfBinaryTree(TreeNode *root)
    {
        queue<TreeNode *> q;
        int ret = 0;
        q.push(root);
        while (!q.empty())
        {
            ret = max<int>(ret, q.size());
            auto count = q.size();
            for (int i = 0; i < count; ++i)
            {
                TreeNode *tmp = q.front();
                q.pop();
                if (tmp)
                {
                    q.push(tmp->left);
                    q.push(tmp->right);
                }
            }
        }
        return ret;
    }
};